package p300;

import java.util.Iterator;
import java.util.List;
import java.util.Stack;

/**
 * @author pengjz <br>
 * @version 1.0 <br>
 * @description Leetcode_331 <br>
 * @date 2021/3/12 22:16 <br>
 */
public class Leetcode_341 implements Iterator<Integer> {

    private Stack<NestedInteger> stack = new Stack<>();

    public Leetcode_341(List<NestedInteger> nestedList) {
        nestedList.forEach(v -> stack.push(v));
    }

    @Override
    public Integer next() {
        return stack.pop().getInteger();
    }

    @Override
    public boolean hasNext() {
        if (stack.empty()) {
            return false;
        } else {
            if (stack.peek().isInteger()) {
                stack.pop().getList().forEach(v -> stack.push(v));
                return hasNext();
            } else {
                return true;
            }
        }
    }
}

interface NestedInteger {

    // @return true if this NestedInteger holds a single integer, rather than a nested list.
    public boolean isInteger();

    // @return the single integer that this NestedInteger holds, if it holds a single integer
// Return null if this NestedInteger holds a nested list
    public Integer getInteger();

    // @return the nested list that this NestedInteger holds, if it holds a nested list
// Return null if this NestedInteger holds a single integer
    public List<NestedInteger> getList();
}
